package models.games;

import java.util.Random;

import core.User;

public class PesciDisorientati extends Game {

	// private static final int NUMBER_OF_FISHES = 10;

	public enum CardinalPoints {
		NORTH, EAST, SOUTH, WEST
	}

	private boolean isDirection;// true se bisogna indovinare la direzione di
								// movimento, false se si deve indovinare in che
								// verso punta il pesce
	private CardinalPoints[] fishesOrientation;// direzione in cui punta il
												// pesce
	private CardinalPoints fishesDirection;// direzione in cui si muove il pesce
	private Random random = new Random();
	private CardinalPoints intruderOrientation;
	private final int DIMENSION = 3;// dimension
	private CardinalPoints[] cardinalVector = CardinalPoints.values();

	public PesciDisorientati(User user) {
		super(user);
		fishesOrientation = new CardinalPoints[DIMENSION * DIMENSION];
		refresh();
	}

	private void setRandomIsDirection() {
		isDirection = random.nextInt(2) == 0 ? false : true;
	}

	public boolean getIsDirection() {
		return isDirection;
	}

	public CardinalPoints[] getFishesOrientations() {
		return fishesOrientation;
	}

	public CardinalPoints getFishesDirection() {
		return fishesDirection;
	}

	private void setRandomFishesDirection() {
		fishesDirection = cardinalVector[random.nextInt(cardinalVector.length)];
	}

	public void refresh() {
		setRandomIsDirection();
		setRandomFishesDirection();
		int intruder = random.nextInt(DIMENSION * DIMENSION);
		int i = random.nextInt(cardinalVector.length);
		intruderOrientation = cardinalVector[i];

		// scambio
		int last = cardinalVector.length - 1;
		CardinalPoints tmp = cardinalVector[last];
		cardinalVector[last] = cardinalVector[i];
		cardinalVector[i] = tmp;
		CardinalPoints remainingOrientation = cardinalVector[random
				.nextInt(cardinalVector.length - 1)];
		for (int k = 0; k < fishesOrientation.length; k++) {
			fishesOrientation[k] = remainingOrientation;
		}

		fishesOrientation[intruder] = intruderOrientation;

	}

	public int getDimension() {
		return DIMENSION;
	}

	public boolean verify(CardinalPoints point) {
		boolean result;
		if (isDirection) {
			result = point == fishesDirection;
		} else
			result = intruderOrientation == point;
		if (result)
			score += 1000;
		return result;
	}

	// public static void main(String[] args) {
	// PesciAffamati pesci = new PesciAffamati(null);
	// Scanner sc = new Scanner(System.in);
	// while(true) {
	// System.out.println(Arrays.toString(pesci.fishes));
	// System.out.print("Inserisci l'indice del pesce selezionato: ");
	// try {
	// int answer = sc.nextInt();
	// pesci.verify(answer);
	// } catch(Exception e) {
	// System.out.println("Hai sbagliato riprova");
	// }
	// }
	//
	// }

	@Override
	public int getGameKey() {
		return 5;
	}

}
